package com.eet.juc.utils;

import java.util.concurrent.*;

/**
 * 减法器辅助类Demo
 *
 * @author zheng.zhang
 */
public class CountDownLatchDemo {
    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(3, 5, 1L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws InterruptedException {
        // 定义计数器数量为6
        CountDownLatch count = new CountDownLatch(6);
        for (int i = 0; i < 6; i++) {
            EXECUTOR.submit(() -> {
                System.out.println(Thread.currentThread().getName().concat(" -> Go out"));
                // 计数器-1
                count.countDown();
            }, String.valueOf(i));
        }
        // 等待计数器归0再执行后面代码
        count.await();

        // 主线程
        System.out.println("Close door");
        EXECUTOR.shutdownNow();
    }
}
